﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;

using System.IO;
using System.Reflection;
using System.Diagnostics;

using System.Threading;
using System.Text.RegularExpressions;

namespace SqlMiM.Common.Sql
{
    /// <summary>
    /// Monitor database. Check if exists, create, and upgrade a user database
    /// </summary>
    public class SqlCeHealth
    {

        /// <summary>
        /// Main Method 
        /// If not Found : Create it and return false;
        /// IF Found : return true;
        /// </summary>
        public static bool VerifyHealth()
        {
            if (!SqlCeHealth.DatabaseExists())
                SqlCeHealth.CreateDatabase();

            // Get a pooled Connexion
            var connexion = SqlCePoolConnection.CreateSqlCePooledConnection(Context.Current.ConfigurationSqlCeConnectionString, true);

            // Create the table if not exists
            SqlCeCommand cmdExistsConfig = SqlCeCommands.CreateCommand(connexion, SqlCeCommands.EXIST_CONFIG_TABLE);
            SqlCeCommand cmdCreateConfigTable = SqlCeCommands.CreateCommand(connexion, SqlCeCommands.CREATE_CONFIG_TABLE);

            try
            {
                connexion.Open();

                int exist = (int)cmdExistsConfig.ExecuteScalar();

                if (exist == 0)
                    cmdCreateConfigTable.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                connexion.Close();
            }

            return true;
        }

  

        /// <summary>
        /// Create a database for one particular User Id
        /// </summary>
        internal static bool CreateDatabase()
        {
            try
            {
                SqlCeEngine engine = new SqlCeEngine(Context.Current.ConfigurationSqlCeConnectionString);

                engine.CreateDatabase();

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check for Database
        /// </summary>
        internal static bool DatabaseExists()
        {
            DirectoryInfo currentDir = GetExecutingDirectory();

            FileInfo[] files = currentDir.GetFiles("SqlMiM.sdf");

            if (files == null || files.Length == 0)
                return false;

            return true;

        }

        /// <summary>
        /// Get Executing Directory  (like "C:\MyPesonnalFolders\MyApplication\" )
        /// </summary>
        internal static DirectoryInfo GetExecutingDirectory()
        {
            DirectoryInfo di = new DirectoryInfo(Assembly.GetExecutingAssembly().Location);

            if (di == null || di.Parent == null || di.Parent.Exists == false)
                throw new Exception("Can't open Directory to check Sql MiM database");

            return di.Parent;
        }


    }

}
